package com.vichat.user.controller;

import com.vichat.common.cache.LocalCacher;
import com.vichat.common.vo.Grid;
import com.vichat.common.vo.Json;
import com.vichat.common.vo.PageFilter;
import com.vichat.user.cache.StaticDataCache;
import com.vichat.user.entity.SysStaticData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.vichat.user.service.IStaticDataService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLDecoder;
import java.util.List;

/**
 * Created by zhangxi on 2017/12/19.
 */
@Controller
@RequestMapping("/vcapi/sys")
public class SysController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;
    @Autowired
    private IStaticDataService staticDataService;

    /**
     * 系统参数
     *
     * @return
     */
    @RequestMapping("/sysStaticData")
    public String orgManagement() {
        return "/sys/sysStaticData";
    }

    /**
     * 查询系统参数
     *
     * @param queryBean
     * @param pf
     * @return
     */
    @RequestMapping("/querySysStaticData")
    @ResponseBody
    public Grid querySysStaticData(SysStaticData queryBean, PageFilter pf) {
        Grid grid = new Grid();
        try {
            pf.setRows(pf.getLimit());
            if (StringUtils.isNotBlank(queryBean.getCodeName())) {
                queryBean.setCodeName(URLDecoder.decode(queryBean.getCodeName(), "UTF-8"));
            }
            List<SysStaticData> sysStaticData = staticDataService.querySysStaticData(queryBean, pf);
            long count = staticDataService.countSysStaticData(queryBean);
            grid.setData(sysStaticData);
            grid.setCount(count);
        } catch (Exception e) {
            e.printStackTrace();
            grid.setMsg(e.getMessage());
            grid.setCode(-9);
        }
        return grid;
    }

    /**
     * 通过codeId获取系统参数
     *
     * @param codeId
     * @return
     */
    @RequestMapping("/queryStaticDataByCodeId")
    @ResponseBody
    public Json queryStaticDataByCodeId(long codeId) {
        Json json = new Json();
        try {
            SysStaticData sysStaticDataFDB = staticDataService.queryStaticDataByCodeId(codeId);
            json.setObj(sysStaticDataFDB);
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }

    /**
     * 保存或修改系统参数
     *
     * @param sysStaticData
     * @return
     */
    @RequestMapping("/saveOrUpdStaticData")
    @ResponseBody
    public Json saveOrUpdStaticData(SysStaticData sysStaticData) {
        Json json = new Json();
        try {
            staticDataService.saveOrUpdStaticData(sysStaticData);
            LocalCacher.refresh(StaticDataCache.cacheId());
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }

    /**
     * 删除系统参数
     *
     * @param codeId
     * @return
     */
    @RequestMapping("/delStaticData")
    @ResponseBody
    public Json delStaticData(long codeId) {
        Json json = new Json();
        try {
            staticDataService.delStaticData(codeId);
            LocalCacher.refresh();
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }

}
